package com.hd.trans.db.greenDao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.hd.trans.db.bean.HuDunLanguage;

import com.hd.trans.db.bean.TranslateRecord;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "TRANSLATE_RECORD".
*/
public class TranslateRecordDao extends AbstractDao<TranslateRecord, Long> {

    public static final String TABLENAME = "TRANSLATE_RECORD";

    /**
     * Properties of entity TranslateRecord.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property IsVoice = new Property(1, boolean.class, "isVoice", false, "is_voice");
        public final static Property LanguageFromId = new Property(2, Long.class, "languageFromId", false, "LANGUAGE_FROM_ID");
        public final static Property LanguageToId = new Property(3, Long.class, "languageToId", false, "LANGUAGE_TO_ID");
        public final static Property TriggerTime = new Property(4, long.class, "triggerTime", false, "trigger_time");
        public final static Property ContentOrig = new Property(5, String.class, "contentOrig", false, "content_orig");
        public final static Property PinyinOrig = new Property(6, String.class, "pinyinOrig", false, "pinyin_org");
        public final static Property ContentTranslate = new Property(7, String.class, "contentTranslate", false, "content_translate");
        public final static Property PinyinTranslate = new Property(8, String.class, "pinyinTranslate", false, "pinyin_translate");
        public final static Property IsCollected = new Property(9, boolean.class, "isCollected", false, "is_collected");
        public final static Property IsReverse = new Property(10, boolean.class, "isReverse", false, "is_reverse");
        public final static Property TranslateState = new Property(11, int.class, "translateState", false, "record_state");
        public final static Property IsNation = new Property(12, boolean.class, "isNation", false, "is_nation");
        public final static Property RecordType = new Property(13, int.class, "recordType", false, "record_type");
        public final static Property VoiceFile = new Property(14, String.class, "voiceFile", false, "voice_file");
    }

    private DaoSession daoSession;


    public TranslateRecordDao(DaoConfig config) {
        super(config);
    }
    
    public TranslateRecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"TRANSLATE_RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"is_voice\" INTEGER NOT NULL ," + // 1: isVoice
                "\"LANGUAGE_FROM_ID\" INTEGER," + // 2: languageFromId
                "\"LANGUAGE_TO_ID\" INTEGER," + // 3: languageToId
                "\"trigger_time\" INTEGER NOT NULL ," + // 4: triggerTime
                "\"content_orig\" TEXT," + // 5: contentOrig
                "\"pinyin_org\" TEXT," + // 6: pinyinOrig
                "\"content_translate\" TEXT," + // 7: contentTranslate
                "\"pinyin_translate\" TEXT," + // 8: pinyinTranslate
                "\"is_collected\" INTEGER NOT NULL ," + // 9: isCollected
                "\"is_reverse\" INTEGER NOT NULL ," + // 10: isReverse
                "\"record_state\" INTEGER NOT NULL ," + // 11: translateState
                "\"is_nation\" INTEGER NOT NULL ," + // 12: isNation
                "\"record_type\" INTEGER NOT NULL ," + // 13: recordType
                "\"voice_file\" TEXT);"); // 14: voiceFile
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"TRANSLATE_RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, TranslateRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getIsVoice() ? 1L: 0L);
 
        Long languageFromId = entity.getLanguageFromId();
        if (languageFromId != null) {
            stmt.bindLong(3, languageFromId);
        }
 
        Long languageToId = entity.getLanguageToId();
        if (languageToId != null) {
            stmt.bindLong(4, languageToId);
        }
        stmt.bindLong(5, entity.getTriggerTime());
 
        String contentOrig = entity.getContentOrig();
        if (contentOrig != null) {
            stmt.bindString(6, contentOrig);
        }
 
        String pinyinOrig = entity.getPinyinOrig();
        if (pinyinOrig != null) {
            stmt.bindString(7, pinyinOrig);
        }
 
        String contentTranslate = entity.getContentTranslate();
        if (contentTranslate != null) {
            stmt.bindString(8, contentTranslate);
        }
 
        String pinyinTranslate = entity.getPinyinTranslate();
        if (pinyinTranslate != null) {
            stmt.bindString(9, pinyinTranslate);
        }
        stmt.bindLong(10, entity.getIsCollected() ? 1L: 0L);
        stmt.bindLong(11, entity.getIsReverse() ? 1L: 0L);
        stmt.bindLong(12, entity.getTranslateState());
        stmt.bindLong(13, entity.getIsNation() ? 1L: 0L);
        stmt.bindLong(14, entity.getRecordType());
 
        String voiceFile = entity.getVoiceFile();
        if (voiceFile != null) {
            stmt.bindString(15, voiceFile);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, TranslateRecord entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindLong(2, entity.getIsVoice() ? 1L: 0L);
 
        Long languageFromId = entity.getLanguageFromId();
        if (languageFromId != null) {
            stmt.bindLong(3, languageFromId);
        }
 
        Long languageToId = entity.getLanguageToId();
        if (languageToId != null) {
            stmt.bindLong(4, languageToId);
        }
        stmt.bindLong(5, entity.getTriggerTime());
 
        String contentOrig = entity.getContentOrig();
        if (contentOrig != null) {
            stmt.bindString(6, contentOrig);
        }
 
        String pinyinOrig = entity.getPinyinOrig();
        if (pinyinOrig != null) {
            stmt.bindString(7, pinyinOrig);
        }
 
        String contentTranslate = entity.getContentTranslate();
        if (contentTranslate != null) {
            stmt.bindString(8, contentTranslate);
        }
 
        String pinyinTranslate = entity.getPinyinTranslate();
        if (pinyinTranslate != null) {
            stmt.bindString(9, pinyinTranslate);
        }
        stmt.bindLong(10, entity.getIsCollected() ? 1L: 0L);
        stmt.bindLong(11, entity.getIsReverse() ? 1L: 0L);
        stmt.bindLong(12, entity.getTranslateState());
        stmt.bindLong(13, entity.getIsNation() ? 1L: 0L);
        stmt.bindLong(14, entity.getRecordType());
 
        String voiceFile = entity.getVoiceFile();
        if (voiceFile != null) {
            stmt.bindString(15, voiceFile);
        }
    }

    @Override
    protected final void attachEntity(TranslateRecord entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public TranslateRecord readEntity(Cursor cursor, int offset) {
        TranslateRecord entity = new TranslateRecord( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getShort(offset + 1) != 0, // isVoice
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // languageFromId
            cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3), // languageToId
            cursor.getLong(offset + 4), // triggerTime
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // contentOrig
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // pinyinOrig
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // contentTranslate
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // pinyinTranslate
            cursor.getShort(offset + 9) != 0, // isCollected
            cursor.getShort(offset + 10) != 0, // isReverse
            cursor.getInt(offset + 11), // translateState
            cursor.getShort(offset + 12) != 0, // isNation
            cursor.getInt(offset + 13), // recordType
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14) // voiceFile
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, TranslateRecord entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setIsVoice(cursor.getShort(offset + 1) != 0);
        entity.setLanguageFromId(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setLanguageToId(cursor.isNull(offset + 3) ? null : cursor.getLong(offset + 3));
        entity.setTriggerTime(cursor.getLong(offset + 4));
        entity.setContentOrig(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setPinyinOrig(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setContentTranslate(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setPinyinTranslate(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setIsCollected(cursor.getShort(offset + 9) != 0);
        entity.setIsReverse(cursor.getShort(offset + 10) != 0);
        entity.setTranslateState(cursor.getInt(offset + 11));
        entity.setIsNation(cursor.getShort(offset + 12) != 0);
        entity.setRecordType(cursor.getInt(offset + 13));
        entity.setVoiceFile(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(TranslateRecord entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(TranslateRecord entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(TranslateRecord entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getHuDunLanguageDao().getAllColumns());
            builder.append(" FROM TRANSLATE_RECORD T");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T0 ON T.\"LANGUAGE_FROM_ID\"=T0.\"_id\"");
            builder.append(" LEFT JOIN HU_DUN_LANGUAGE T1 ON T.\"LANGUAGE_TO_ID\"=T1.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected TranslateRecord loadCurrentDeep(Cursor cursor, boolean lock) {
        TranslateRecord entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        HuDunLanguage languageFrom = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setLanguageFrom(languageFrom);
        offset += daoSession.getHuDunLanguageDao().getAllColumns().length;

        HuDunLanguage languageTo = loadCurrentOther(daoSession.getHuDunLanguageDao(), cursor, offset);
        entity.setLanguageTo(languageTo);

        return entity;    
    }

    public TranslateRecord loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<TranslateRecord> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<TranslateRecord> list = new ArrayList<TranslateRecord>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<TranslateRecord> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<TranslateRecord> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
